import os

from ppgnss import gnss_time

rtcm_dir = r"F:\Research\plant_growth\data\rtcm"
rinex_dir = r"F:\Research\plant_growth\data\rnx"
convbin = r"e:\ProgramSrc\b34-bin\convbin.exe"

for fn in os.listdir(rtcm_dir):
    if not fn.endswith("RTCM3"):
        continue
    site = fn.split(".")[0]
    year = int(fn[7:11])
    doy = int(fn[11:14])
    _, mo, dy = gnss_time.doy2ymd(year, doy)
    filename = os.path.join(rtcm_dir, fn)
    # print(site, year, doy, fn)
    outfile = os.path.join(rinex_dir, "%s%03d0.%02do" %(site[-4:], doy, year-2000) )
    approx_time = "%04d/%02d/%02d 00:00:00" %(year, mo, dy)
    cmd = "%s -ti 30 -r rtcm3 %s -tr %s -v 3.04 -f 8 -os -od -o %s" %(
        convbin, filename, approx_time, outfile)
    print(cmd)
    os.system(cmd)